package com.likeycy.my;

import com.likeycy.my.file.FileInfo;
import com.likeycy.my.file.Files;
import com.likeycy.my.set.MyListSet;
import com.likeycy.my.set.MySet;
import com.likeycy.my.set.MyTreeSet;

import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName: TestMySet
 * @Description: 测试
 * @Author: sodagreen
 * @Date: 2021/3/24 23:38
 * @Version: 1.0
 */
public class TestMySet {

    public static void test1(){
        MySet<Integer> listSet = new MyListSet<>();
        listSet.add(10);
        listSet.add(11);
        listSet.add(11);
        listSet.add(12);
        listSet.add(10);

        MySet<Integer> treeSet = new MyTreeSet<>();
        treeSet.add(12);
        treeSet.add(10);
        treeSet.add(7);
        treeSet.add(11);
        treeSet.add(10);
        treeSet.add(11);
        treeSet.add(9);

        treeSet.traversal(new MySet.Visitor<Integer>() {
            @Override
            public boolean visit(Integer element) {
                System.out.println(element);
                return false;
            }
        });
    }

    public static void testMySetByFor(MySet<String> mySet, String[] words) {
        for (int i = 0; i < words.length; i++) {
            mySet.add(words[i]);
        }
        System.out.println("不重复单词数量：" + mySet.size());
        for (int i = 0; i < words.length; i++) {
            mySet.contains(words[i]);
        }
        for (int i = 0; i < words.length; i++) {
            mySet.remove(words[i]);
        }
    }

    /**
     * 读取文件内的单词数量去重
     */
    public static void test2() {
        FileInfo fileInfo = Files.read("D:\\soft\\Java\\openjdk-1.8\\src\\java\\util\\concurrent"
                , new String[]{"java"});
        System.out.println("文件数量：" + fileInfo.getFiles());
        System.out.println("代码行数：" + fileInfo.getLines());
        String[] words = fileInfo.words();
        System.out.println("单词总数：" + words.length);


        TimeTool.check("ListSet", new TimeTool.Task() {
            @Override
            public void execute() {
                testMySetByFor(new MyListSet<>(), words);
            }
        });

        TimeTool.check("TreeSet", new TimeTool.Task() {
            @Override
            public void execute() {
                testMySetByFor(new MyTreeSet<>(), words);
            }
        });

    }

    public static void main(String[] args) {
//        test2()

        int[] nums1 = new int[] {
                1,2,2,1
        };
        int[] nums2 = new int[] {
                2,2
        };

        if(nums1.length == 0 || nums2.length == 0) {
            return;
        }

        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();
        for (int i : nums1) {
            set1.add(i);
        }
        for (int i : nums2) {
            if (set1.contains(i)) {
                set2.add(i);
            }
        }
        int[] num3 = new int[set2.size()];
        int j = 0;
        for (Integer integer : set2) {
            num3[j++] = integer;
        }
    }


}
